package com.iflytek.jcy.supervise.job.component.converter;

import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.mapstruct.InjectionStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import com.iflytek.jcy.supervise.job.component.httpinterface.result.OuterDeptNew;
import com.iflytek.jcy.supervise.job.component.httpinterface.result.OuterDeptPerson;
import com.iflytek.sec.uap.dto.organization.OrgExtendImportDto;
import com.iflytek.sec.uap.dto.user.BatchSaveUserDto;

/**
 * @author <a href=mailto:ktyi@iflytek.com>伊开堂</a>
 * @date 2022/1/18
 */
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.CONSTRUCTOR, imports = {StringUtils.class,
        CollectionUtils.class, Collectors.class, FastDateFormat.class, Long.class, DurationFormatUtils.class,
        FilenameUtils.class})
public abstract class BeanConverter extends BaseConverter {
    @Mapping(source = "dwbm", target = "uapOrg.id")
    @Mapping(source = "dwmc", target = "uapOrg.name")
    @Mapping(source = "dwbm", target = "uapOrg.code")
    @Mapping(source = "fdwbm", target = "higherCode")
    @Mapping(source = "fdwbm", target = "uapOrg.higherOrg")
    @Mapping(source = "dwjc", target = "uapOrg.shortName")
    @Mapping(source = "dwjb", target = "uapOrg.level")
    public abstract OrgExtendImportDto fromOuter(OuterDeptNew outerDeptNew);
    
    
    @Mapping(source = "dwbm", target = "uapOrg.code")
    @Mapping(source = "dwmc", target = "uapOrg.name")
    @Mapping(source = "rybm", target = "user.loginName")
    @Mapping(source = "mc", target = "user.name")
    @Mapping(source = "dwbm", target = "user.orgCode")
    @Mapping(source = "dwmc", target = "user.orgName")
    public abstract BatchSaveUserDto fromOuterPerson(OuterDeptPerson outerDeptPerson);

    public static class CustomConverter {
        public static String parseDuration(String sec) {
            int seconds = Integer.parseInt(sec);
            String format = "ss'秒'";
            if (seconds >= 60) {
                format = "mm'分'ss'秒'";
            }
            return DurationFormatUtils.formatDuration(seconds * 1000L, format, false);
        }

        public static Long parseDateStr(String dateStr) {
            if (StringUtils.isBlank(dateStr)) {
                return null;
            }

            Instant instant = null;
            try {
                instant = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(dateStr, Instant::from);
            }
            catch (Exception e) {
                try {
                    instant = DateTimeFormatter.ISO_INSTANT.parse(dateStr, Instant::from);
                }
                catch (Exception ex) {
                    return null;
                }
            }
            return instant.toEpochMilli();
        }
    }

}
